package com.fuyao.cloud.daemon.quartz.util;

import cn.hutool.core.util.StrUtil;
import com.fuyao.cloud.common.core.util.SpringContextHolder;
import com.fuyao.cloud.daemon.quartz.constants.JobTypeQuartzEnum;
import com.fuyao.cloud.daemon.quartz.exception.TaskException;
import lombok.extern.slf4j.Slf4j;

/**
 * TaskInvoker工厂类
 * @author Hccake
 * @version 1.0
 * @date 2019/8/8 15:40
 */
@Slf4j
public class TaskInvokerFactory {

    /**
     * 根据对应 jobType 获取对应 invoker
     *
     * @param jobType 任务类型
     * @return
     * @throws TaskException
     */
    public static ITaskInvoker getInvoker(String jobType) throws TaskException {
        if (StrUtil.isBlank(jobType)) {
            throw new TaskException("定时任务类型为空");
        }

        ITaskInvoker iTaskInvoker;
        if (JobTypeQuartzEnum.JAVA.getType().equals(jobType)) {
            iTaskInvoker = SpringContextHolder.getBean(JobTypeQuartzEnum.JAVA.getExecutor());
        } else if (JobTypeQuartzEnum.SPRING_BEAN.getType().equals(jobType)) {
            iTaskInvoker = SpringContextHolder.getBean(JobTypeQuartzEnum.SPRING_BEAN.getExecutor());
        } else if (JobTypeQuartzEnum.REST.getType().equals(jobType)) {
            iTaskInvoker = SpringContextHolder.getBean(JobTypeQuartzEnum.REST.getExecutor());
        } else if (JobTypeQuartzEnum.JAR.getType().equals(jobType)) {
            iTaskInvoker = SpringContextHolder.getBean(JobTypeQuartzEnum.JAR.getExecutor());
        } else {
            throw new TaskException(String.format("无法获取设定的定时任务类型:[%s]", jobType));
        }

        return iTaskInvoker;
    }

}
